package com.hayato.demo.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.hayato.demo.dao.ProductDao;
import com.hayato.demo.client.Info.OrderInfo;
import com.hayato.demo.client.Info.OrderProductInfo;
import com.hayato.demo.client.Info.ProductInfo;
import com.hayato.demo.utils.Constant;

/**
 * Servlet implementation class ProductSearchServlet
 */
public class ProductSearchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	String page = Constant.DISP_ERROR;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet( HttpServletRequest request, HttpServletResponse response )
			throws ServletException, IOException {
		doPost( request, response );
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost( HttpServletRequest request, HttpServletResponse response )
			throws ServletException, IOException {
		request.setCharacterEncoding( Constant.ENCODING );
		String action = request.getParameter( "action" );

		if ( !StringUtils.isEmpty( action ) ) {
			if ( action.equals( "searchProduct" ) ) {
				searchProductByCategory2( request, response );
			} else if ( action.equals( Constant.ACTION_SEARCH ) ) {
				searchProductByName( request, response );
			}
		}

		request.getRequestDispatcher( page ).forward( request, response );
	}

	String customerId = "";
	String deliveryDate = "";
	List<ProductInfo> listProduct;

	private void searchProductByCategory2( HttpServletRequest request,
			HttpServletResponse response ) throws ServletException {

		String category2Id = request.getParameter( "category2Id" );
		customerId = request.getParameter( "customerId" );
		deliveryDate = request.getParameter( "deliveryDate" );

		ProductDao productDao = new ProductDao();

		try {
			listProduct = productDao.findAllProductInCategory2( Long
					.parseLong( category2Id ) );

			request.setAttribute( "listProduct", listProduct );
			request.setAttribute( "customerId", customerId );
			request.setAttribute( "deliveryDate", deliveryDate );

			page = Constant.DISP_PRODUCT_LIST;

		} catch ( NumberFormatException pe ) {
			page = Constant.DISP_ERROR;
		}
	}

	private void searchProductByName( HttpServletRequest request,
			HttpServletResponse response ) throws ServletException {

		String productName = request.getParameter( "productName" );
		customerId = request.getParameter( "customerId" );
		deliveryDate = request.getParameter( "deliveryDate" );

		ProductDao productDao = new ProductDao();

		listProduct = productDao.findProductByName( productName );

		request.setAttribute( "listProduct", listProduct );
		request.setAttribute( "customerId", customerId );
		request.setAttribute( "deliveryDate", deliveryDate );

		page = Constant.DISP_PRODUCT_LIST;
	}
}
